package longrise.phone.com.bjjt_henan.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;

import longrise.phone.com.bjjt_henan.Adapter.WeacherAdapter;
import longrise.phone.com.bjjt_henan.R;

/**
 * Created by Administrator on 2016/5/19.
 */
public class WeacherDialog extends Dialog {

    private Context mContext;

    private TextView curTV;

    private TextView curweaTV;

    private TextView tempTV;

    private TextView addTV;

    private TextView dateTV;

    private TextView dayTV;

    private GridView gridView;

    private WeacherAdapter adapter;

    public WeacherDialog(Context context) {
        super(context, R.style.AlertDialogStyle);
        mContext = context;

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_weacher, null);
        initViews(view);
        setContentView(view);
        //设置大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.80);
        dialogWindow.setAttributes(lp);
    }

    private void initViews(View view) {
        curTV = (TextView) view.findViewById(R.id.weacher_tv_curtemp);
        curweaTV = (TextView) view.findViewById(R.id.weacher_tv_curwea);
        tempTV = (TextView) view.findViewById(R.id.weacher_tv_temp);
        addTV = (TextView) view.findViewById(R.id.weacher_tv_address);
        dateTV = (TextView) view.findViewById(R.id.weacher_tv_date);
        dayTV = (TextView) view.findViewById(R.id.weacher_tv_day);
        gridView = (GridView) view.findViewById(R.id.weacher_gv);

        adapter = new WeacherAdapter(mContext);
        gridView.setAdapter(adapter);
    }

    public void setData(EntityBean bean) {
        try {
            addTV.setText(bean.getString("currentCity"));
            EntityBean[] weathers = bean.getBeans("weather");
            if (null != weathers && weathers.length > 0) {
                //当天天气
                EntityBean curBean = weathers[0];
                curweaTV.setText(curBean.getString("weather"));
                tempTV.setText(getWeacherStr(curBean.getString("temperature")));
                //未来天气
                gridView.setNumColumns(weathers.length - 1);
                adapter.setLists(weathers);
                adapter.notifyDataSetChanged();

                String date = curBean.getString("date");
                String[] dates = date.split(" ");
                if (dates != null) {
                    dayTV.setText(dates[0]);
                    if (dates.length > 1) {
                        dateTV.setText(dates[1]);
                        String curtemp = dates[2];
                        int start = curtemp.indexOf("：");
                        int end = curtemp.indexOf("℃");
                        curTV.setText(curtemp.substring(start + 1, end) + "°");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调换天气的温度从低到高
     *
     * @param info
     * @return
     */
    public static String getWeacherStr(String info) {
        try {
            if (TextUtils.isEmpty(info)) {
                return null;
            }
            int index = info.indexOf("~");
            String high = info.substring(0, index);
            String low = info.substring(index + 1, info.indexOf("℃"));
            return low + " ~ " + high + "℃";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }
}
